﻿using Microsoft.Devices;
using Microsoft.Phone;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.IO;
using System;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace HOPE {
	public class GameplayScreenTest: GameScreen {

		#region Fields and Properties

		SpriteBatch spriteBatch;
		Texture2D backgroundTexture;
		ContentManager content;
		string backgroundTextureFile;
		Texture2D _controlstickBoundary, _controlstick;
		Vector2 _controlstickpos = new Vector2(55, 369);
		Vector2 _controlstickBoundarypos = new Vector2(34, 347);
		public static Texture2D _jumpbutton;
		public static Vector2 _jumpbuttonpos = new Vector2(690, 370);
		public static bool _isJumpButtonClicked;

		// a texture used to draw the Jump button on the screen
		public static Texture2D _actionbutton;
		public static Vector2 _actionbuttonpos = new Vector2(610, 380);
		public static bool _isActionButtonClicked;
		public static bool _isActionButtonActive;

		private GamePadState gamePadState;
		private KeyboardState keyboardState;
		private TouchCollection touchState;
		private DisplayOrientation displayOrientation;
		private Texture2D pauseOverlay;

		private bool wasContinuePressed;
		
		private Texture2D _playerpanel;
		private Vector2 _playerpanelpos;

	
		
		public string BackgroundTextureFile {
			get { return backgroundTextureFile; }
			//set { backgroundTextureFile = value; }
		}

		#endregion

		public GameplayScreenTest (string backgroundFilename) {
			TransitionOnTime = TimeSpan.FromSeconds(0.5);
			TransitionOffTime = TimeSpan.FromSeconds(0.5);
			backgroundTextureFile = backgroundFilename;
		}

		public override void LoadContent () {
			if (content == null) content = new ContentManager(ScreenManager.Game.Services, "Content");
			backgroundTexture=content.Load<Texture2D>(backgroundTextureFile);
			LoadThumbstickContent();
			_playerpanel = content.Load<Texture2D>("Textures/Interface/StatusBar/Panel/playerpanel");
			base.LoadContent();
		}

		private void LoadThumbstickContent () {
			//load control
			_controlstick = content.Load<Texture2D>("Textures/GamePlay/Control/controlstick");
			_controlstickBoundary = content.Load<Texture2D>("Textures/Gameplay/Control/controlstickBoundary");
			_jumpbutton = content.Load<Texture2D>("Textures/Gameplay/Control/jumpbutton");
			_actionbutton = content.Load<Texture2D>("Textures/Gameplay/Control/actionbutton");
		}

		#region Per-frame methods
		public override void Update (GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) {
			VirtualThumbsticks.Update();
			HandleThumbstick();
			base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
		}

		private void DrawJumpButton () {
			if (_isJumpButtonClicked) {
				spriteBatch.Draw(_jumpbutton, new Rectangle((int)_jumpbuttonpos.X,
					(int)_jumpbuttonpos.Y, 70, 70), new Rectangle(70, 0, 70, 70),
					Color.White);
			}
			else {
				spriteBatch.Draw(_jumpbutton, new Rectangle((int)_jumpbuttonpos.X,
					(int)_jumpbuttonpos.Y, 70, 70), new Rectangle(0, 0, 70, 70),
					Color.White);
			}
		}
		private  void DrawActionButton () {
			//ketika tidak ada sesuatu yang bisa mentrigger action button, dia akan mati
			if (_isActionButtonActive)
				spriteBatch.Draw(_actionbutton, _actionbuttonpos, Color.White);
			else
				spriteBatch.Draw(_actionbutton, _actionbuttonpos, Color.Black);

			if (_isActionButtonClicked)
				spriteBatch.Draw(_actionbutton, new Rectangle((int)_actionbuttonpos.X,
					(int)_actionbuttonpos.Y, 70, 70), new Rectangle(70, 0, 70, 70),
					Color.White);
			else
				spriteBatch.Draw(_actionbutton, new Rectangle((int)_actionbuttonpos.X,
					(int)_actionbuttonpos.Y, 70, 70), new Rectangle(0, 0, 70, 70),
					Color.White);
		}
		private  void DrawThumbstick () {
			spriteBatch.Draw(_controlstickBoundary, _controlstickBoundarypos, Color.White);
			spriteBatch.Draw(_controlstick, _controlstickpos, Color.White);
		}
		public override void HandleInput (InputState input) {
			if (input == null)
				throw new ArgumentNullException("input");

			// Look up inputs for the active player profile.
			int playerIndex = (int)ControllingPlayer.Value;

			//get all of our input states
			keyboardState = Keyboard.GetState();
			gamePadState = GamePad.GetState(PlayerIndex.One);
			touchState = TouchPanel.GetState();

			//pause the game when bac is pressed
			if (gamePadState.Buttons.Back == ButtonState.Pressed)
				spriteBatch.Draw(pauseOverlay, new Vector2(0, 0), Color.White);

			bool continuePressed =
				keyboardState.IsKeyDown(Keys.Space) ||
				gamePadState.IsButtonDown(Buttons.A) ||
				touchState.AnyTouch();

			//set our button is always reset to inactive state
			_isJumpButtonClicked = false;
			_isActionButtonClicked = false;

			// Perform the appropriate action to advance the game and
			// to get the player back to playing.
			if (!wasContinuePressed && continuePressed) {
				//if (!level.Player.IsAlive) {
				//    level.StartNewLife();
				//}
				//else if (level.TimeRemaining == TimeSpan.Zero) {
				//    if (level.ReachedExit)
				//        LoadNextLevel();
					//else
					//    ReloadCurrentLevel();
				//}
			}

			wasContinuePressed = continuePressed;

		}

		private void DrawHud () {

			//draw panel 
			spriteBatch.Draw(_playerpanel, _playerpanelpos, null, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.None, 0.99f);

			//player panel
			//spriteBatch.Draw(davinaaktif, davinaaktifpos, null, Color.White, 0, new Vector2(0, 0), new Vector2(0.8f, 0.8f), SpriteEffects.None, 0.96f);
			//spriteBatch.Draw(latifupasif, latifupasifpos, null, Color.White, 0, new Vector2(0, 0), new Vector2(0.8f, 0.8f), SpriteEffects.None, 0.95f);
			//spriteBatch.Draw(_zakhaevpasif, _zakhaevaktifpos, null, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.None, 0.92f);
			//spriteBatch.Draw(zakhaevpasif, zakhaevpasifpos, null, Color.White, 0, new Vector2(0, 0), new Vector2(0.6f, 0.6f), SpriteEffects.None, 0.94f);
			//spriteBatch.Draw(audriannapasif, audriannaaktifpos, null, Color.White, 0, new Vector2(0, 0), new Vector2(0.6f, 0.6f), SpriteEffects.None, 0.94f);
			//spriteBatch.Draw(_audriannapasif, _audriannapasifpos, null, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.None, 0.93f);
		}


		public override void Draw (GameTime gameTime) {
			spriteBatch = ScreenManager.SpriteBatch;
			spriteBatch.Begin();
			spriteBatch.Draw(backgroundTexture, new Vector2(0, 0),
	 new Color(255, 255, 255, TransitionAlpha));
			DrawJumpButton();
			DrawActionButton();
			DrawThumbstick();
			DrawHud();
			spriteBatch.End();

			
			base.Draw(gameTime);
		}
		//6. Add Handle thumbstick method and call it from Update()
		//This method is responsible for animating onscreen Thumbstick
		private void HandleThumbstick () {
			//calculate the rectangle of the outer circle of the thumbstick
			Rectangle outerControlstick = new Rectangle(0, (int)_controlstickBoundarypos.Y - 35,
				_controlstickBoundary.Width + 60, _controlstickBoundary.Height + 60);

			//reset the thumbstick position when it's idle
			if (VirtualThumbsticks.LeftThumbstick == Vector2.Zero) {
				_controlstickpos = new Vector2(55, 369); //45,399
			}
			else {
				//moves the thumbstick's inner circle
				float radius = _controlstick.Width / 2 + 35;
				_controlstickpos = new Vector2(55, 369) + (VirtualThumbsticks.LeftThumbstick * radius / 2); //45,369
			}
		}

		#endregion
	}
}